package com.itheima.tlias.service.impl;

import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.model.entity.Dept;
import com.itheima.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Lee
 * @created 2024/4/7
 * @describtion
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    /**
     * 根据部门id修改部门数据
     *
     * @param dept id必须传递
     */
    @Override
    public void updateDeptById(Dept dept) {
        //1.参数合法性校验
        if (ObjectUtils.isEmpty(dept.getName())
                || ObjectUtils.isEmpty(dept.getId())) {
            return;
        }
        // 2. 调用Mapper 接口查询数据库
        deptMapper.updateById(dept);


    }

    /**
     * 获取部门id数据回显
     *
     * @param id 部门id
     * @return
     */
    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.selectById(id);
    }

    /**
     * 添加部门
     *
     * @param dept
     */
    @Override
    public void save(Dept dept) {
        //1.参数处理
        if (ObjectUtils.isEmpty(dept.getName())) {
            return;
        }
        //2.业务处理：补全数据
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.inserDept(dept);

    }

    /**
     * @param id 部门id
     */
    @Override
    public void delById(Integer id) {
        deptMapper.deleteById(id);
    }

    /**
     * 获取部门所有的数据信息
     *
     * @return
     */
    @Override
    public List<Dept> getDepts() {
        return deptMapper.selectDepts();
    }
}
